﻿#region << 文件说明 >>

/*----------------------------------------------------------------
 * 命名空间：Vampirewal.BlogCore
 * 唯一标识：dd07650b-dae1-444e-a177-a938761e9ed9
 * 文件名：PageBase
 *
 * 创建者：杨程
 * 电子邮箱：235160615@qq.com
 * 创建时间：2024/6/12 10:11:48
 * 版本：V1.0.0
 * 描述：
 *
 * ----------------------------------------------------------------
 * 修改人：
 * 时间：
 * 修改说明：
 *
 * 版本：V1.0.1
 *----------------------------------------------------------------*/

#endregion << 文件说明 >>

namespace Vampirewal.BlogCore;

/// <summary>
/// 页面基类
/// </summary>
public abstract class PageBase
{
    /// <summary>
    /// 是否缓存
    /// <para>true是第一次构建html后就一直用</para>
    /// <para>false是每次加载的时候都会构建一次，可以用于调用数据库或其他请求获取数据</para>
    /// <para>如果 IsCheckAuth 属性为true，那么就不能使用缓存 </para>
    /// </summary>
    public abstract bool IsCache { get; }

    /// <summary>
    /// 页面标题
    /// </summary>
    public abstract string Title { get; }

    /// <summary>
    /// 页面归属菜单分组
    /// </summary>
    public abstract string MenuGroup { get; }

    /// <summary>
    /// 菜单分组排序，值越小越靠前
    /// </summary>
    public abstract int MenuGroupSort { get; }

    /// <summary>
    /// 页面加载排序，值越小越靠前
    /// <parm>从0开始</parm>
    /// </summary>
    public abstract int Sort { get; }

    /// <summary>
    /// 标签集合
    /// </summary>
    public abstract List<Tag> Tags { get; }

    /// <summary>
    /// 分组内容
    /// </summary>
    public abstract List<PartContainer> PartContainers { get; }

    internal string ToHtml()
    {
        StringBuilder sb = new StringBuilder();

        sb.AppendLine($"<div>");
        sb.AppendLine($"    <el-row>");

        #region 博客主要内容
        sb.AppendLine($"        <el-col :span=\"18\">");
        sb.AppendLine($"            <div>");

        sb.AppendLine($"                <h1>");
        //sb.AppendLine($"<a class=\"header-anchor vp-link\" style=\"background-color:transparent;touch-action:manipulation;font-weight:500;text-decoration:inherit;color: #409eff;position:absolute;margin-top:.125em;margin-left:-.87em;padding-right:.23em;font-size:.85em;opacity:0;display:inline-flex;align-items:center;white-space:nowrap;\" href=\"#{Title}\" aria-hidden=\"true\">#</a> ");
        sb.AppendLine($"<a class=\"header-anchor vp-link\"  href=\"#{Title}\" aria-hidden=\"true\">#</a> ");

        sb.AppendLine($"                {Title}");
        sb.AppendLine($"                </h1>");
        sb.AppendLine($"                <h5>{MenuGroup}/{Title}</h5>");
        sb.AppendLine($"                <el-space wrap :size=\"5\">");
        foreach (var item in Tags)
        {
            sb.AppendLine($"                    {item.ConverToHtml()}");
        }
        sb.AppendLine($"                </el-space>");
        sb.AppendLine($"            </div>");
        sb.AppendLine($"<el-divider />");
        sb.AppendLine($"            <div ref=\"containerRef\" style=\"overflow-y: auto\">");

        //List<string> PartTitle = new List<string>();

        List<PartTitle> PartTitles = new List<PartTitle>();

        Action<ContainerControlBase, int> act = null;

        act = new Action<ContainerControlBase, int>((cb, margin) =>
        {
            PartTitle pt = new PartTitle()
            {
                MarginLeft = margin,
                Title = cb.Title,
            };
            PartTitles.Add(pt);

            var containerChild = cb.Children.Where(w => w is PartContainer).Select(s => s as ContainerControlBase).ToList();

            foreach (var item in containerChild)
            {
                act.Invoke(item, margin + 10);
            }

        });

        //Part
        foreach (var item in PartContainers)
        {
            sb.AppendLine($"                {item.ConverToHtml()}");
            //PartTitle.Add(item.Title);
            //PartTitle pt = new PartTitle()
            //{
            //    MarginLeft = 0,
            //    Title = item.Title,
            //};
            //PartTitles.Add(pt);

            //var containerChild = item.Children.Where(w => w is PartContainer pc).Select(s=>s as ContainerControlBase).ToList();
            //while (containerChild != null && containerChild.Count > 0)
            //{

            //}

            act.Invoke(item, 0);
        }

        sb.AppendLine($"            </div>");
        sb.AppendLine($"        </el-col>");
        #endregion

        sb.AppendLine($"        <el-col :span=\"6\">");
        sb.AppendLine($"            <el-affix :offset=\"60\">");
        sb.AppendLine($"                <el-anchor :offset=\"70\" style=\"width: 300px\" type=\"underline\">");

        //foreach (var item in PartTitle)
        //{
        //    sb.AppendLine($"                <el-anchor-link href=\"#{item}\" title=\"{item}\"/>");
        //}

        foreach (var item in PartTitles)
        {
            sb.AppendLine($"                <el-anchor-link href=\"#{item.Title}\" title=\"{item.Title}\" style=\"margin-left: {item.MarginLeft}px;  \"   />");
        }

        sb.AppendLine($"                </el-anchor>");
        sb.AppendLine($"            </el-affix>");

        sb.AppendLine($"        </el-col>");

        sb.AppendLine($"    </el-row>");
        sb.AppendLine($"</div>");
        sb.AppendLine($"");




        return sb.ToString();
    }

}

internal class PartTitle
{
    public int MarginLeft { get; set; }

    public string Title { get; set; }
}